package kcp;

import com.backblaze.erasure.FecAdapt;
import lombok.Getter;
import lombok.Setter;
import threadPool.IMessageExecutorPool;
import threadPool.netty.NettyMessageExecutorPool;

/** Created by JinMiao 2018/9/20. */
@Getter
public class ChannelConfig {
  public static final int crc32Size = 4;

  @Setter private int conv;
  private boolean noDelay;
  private int interval = Kcp.IKCP_INTERVAL;
  private int fastResend;
  private boolean nocwnd;
  @Setter private int sndwnd = Kcp.IKCP_WND_SND;
  @Setter private int rcvwnd = Kcp.IKCP_WND_RCV;
  @Setter private int mtu = Kcp.IKCP_MTU_DEF;
  // 超时时间 超过一段时间没收到消息断开连接
  @Setter private long timeoutMillis;
  // TODO 可能有bug还未测试
  private boolean stream;

  // 下面为新增参数
  @Setter private FecAdapt fecAdapt;
  // 收到包立刻回传ack包
  @Setter private boolean ackNoDelay = false;
  // 发送包立即调用flush 延迟低一些  cpu增加  如果interval值很小 建议关闭该参数
  private final boolean fastFlush = true;
  // crc32校验
  @Setter private boolean crc32Check = false;
  // 接收窗口大小(字节 -1不限制)
  private final int readBufferSize = -1;
  // 发送窗口大小(字节 -1不限制)
  @Setter private int writeBufferSize = -1;

  // 增加ack包回复成功率 填 /8/16/32
  @Setter private int ackMaskSize = 0;

  /** 使用conv确定一个channel 还是使用 socketAddress确定一个channel */
  @Setter private boolean useConvChannel = false;

  /** 处理kcp消息接收和发送的线程池 */
  private IMessageExecutorPool iMessageExecutorPool =
      new NettyMessageExecutorPool(Runtime.getRuntime().availableProcessors());

  public void nodelay(boolean nodelay, int interval, int resend, boolean nc) {
    this.noDelay = nodelay;
    this.interval = interval;
    this.fastResend = resend;
    this.nocwnd = nc;
  }

  public void setiMessageExecutorPool(IMessageExecutorPool iMessageExecutorPool) {
    if (this.iMessageExecutorPool != null) {
      this.iMessageExecutorPool.stop();
    }
    this.iMessageExecutorPool = iMessageExecutorPool;
  }
}
